package com.soft.base.util;

import java.util.*;
/**
 * class
 *
 * @author user
 * @date 2020/06/05
 */
public class TimezoneUtil {

    /**
     * 将date转换为指定时区timezone的时间
     *
     * @param date
     * @param timezone
     * @return Date
     */
    public static Date adjust(Date date, Integer timezone) {
        return adjust(date, timezone, +8);
    }

    /**
     * 将baseTimezone时区的date转换为指定时区timezone的时间
     *
     * @param date
     * @param timezone
     * @param baseTimezone
     * @return Date
     */
    public static Date adjust(Date date, Integer timezone, Integer baseTimezone) {
        return new Date(date.getTime() - (baseTimezone - timezone) * 3600 * 1000L);
    }

    /**
     * 将指定时区timezone的时间转换为当前时区的时间
     *
     * @param date
     * @param timezone
     * @return
     */
    public static Date revert(Date date, Integer timezone) {
        return revert(date, timezone, +8);
    }

    /**
     * 将指定时区timezone的时间复原为baseTimezone的时间
     *
     * @param date
     * @param timezone
     * @param baseTimezone
     * @return
     */
    public static Date revert(Date date, Integer timezone, Integer baseTimezone) {
        return new Date(date.getTime() + (baseTimezone - timezone) * 3600 * 1000L);
    }

    /*
    timezone	time(timezone)	    calc                date(timezone)	    time(+8)	hour diff
    -12	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/16 1:00	    49	        +49
    -11	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/16 0:00	    48	        +48
    -10	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 23:00	47	        +47
    -9	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 22:00	46	        +46
    -8	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 21:00	45	        +45
    -7	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 20:00	44	        +44
    -6	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 19:00	43	        +43
    -5	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 18:00	42	        +42
    -4	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 17:00	41	        +41
    -3	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 16:00	40	        +40
    -2	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 15:00	39	        +39
    -1	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 14:00	38	        +38
    0	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 13:00	37	        +37
    +1	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 12:00	36	        +36
    +2	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 11:00	35	        +35
    +3	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 10:00	34	        +34
    +4	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 9:00	    33	        +33
    +5	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 8:00	    32	        +32
    +6	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 7:00	    31	        +31
    +7	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 6:00	    30	        +30
    +8	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 5:00	    29	        +29
    +9	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 4:00	    28	        +28
    +10	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 3:00	    27	        +27
    +11	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 2:00	    26	        +26
    +12	        2018/12/15 5:00	    2018/12/14 0:00	    2018/12/15 1:00	    25	        +25
    */

    /**
     * 获取当前时区时间整点所命中的时区
     *
     * @param date
     * @param targetHour
     * @return
     */
    public static List<Integer> calcTimezones(Date date, int targetHour) {
        return calcTimezones(date, targetHour, +8);
    }

    /**
     * 获取整点所命中的时区
     *
     * @param date
     * @param targetHour
     * @param baseTimezone
     * @return
     */
    public static List<Integer> calcTimezones(Date date, int targetHour, Integer baseTimezone) {
        List<Integer> timezones = new ArrayList<Integer>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int timezone = baseTimezone + targetHour - hour;
        int timezoneValue = 12;
        int otherTimezone = -12;
        if (timezone > timezoneValue) {
            timezone -= 24;
        } else if (timezone < otherTimezone) {
            timezone += 24;
        }
        timezones.add(timezone);
        if (timezone == timezoneValue || timezone == otherTimezone) {
            timezones.add(-timezone);
        }
        return timezones;
    }
}
